package com.example.usercenter.sys.entity;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

public class User implements UserDetails {
    //ID
    private Long id;
    //账号
    private String username;
    //密码
    private String password;
    //用户组
    private UserGroup group;
    //姓名
    private String fullName;
    //性别（男；女）
    private String sex;
    //生日
    private Date birthday;
    //头像
    private String avatar;
    //地区编码
    private String regionCode;
    //街道地址
    private String street;
    //详细地址
    private String fullAddress;
    //启用/禁用（1：启用；0：禁用）
    private boolean enabled;
    //用户角色
    private List<Role> roles;
    //用户权限
    private List<Authority> auths;
    //用户组织
    private List<OrgUser> orgUsers;

    public User(){}

    public User(long id){
        this.id = id;
    }

    public Long getId(){
        return id;
    }

    public void setId(Long id){
        this.id = id;
    }

    @Override
    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    @JsonIgnore
    public String getPassword(){
        return password;
    }

    @JsonProperty
    public void setPassword(String password){
        this.password = password;
    }

    public UserGroup getGroup() {
        return group;
    }

    public void setGroup(UserGroup group) {
        this.group = group;
    }

    public String getFullName() {
        return fullName;
    }

    public void setFullName(String fullName) {
        this.fullName = fullName;
    }

    public String getSex(){
        return sex;
    }

    public void setSex(String sex){
        this.sex = sex;
    }

    public Date getBirthday(){
        return birthday;
    }

    public void setBirthday(Date birthday){
        this.birthday = birthday;
    }

    public String getAvatar() {
        return avatar;
    }

    public void setAvatar(String avatar) {
        this.avatar = avatar;
    }

    public String getRegionCode(){
        return regionCode;
    }

    public void setRegionCode(String regionCode){
        this.regionCode = regionCode;
    }

    public String getStreet(){
        return street;
    }

    public void setStreet(String street){
        this.street = street;
    }

    public String getFullAddress(){
        return fullAddress;
    }

    public void setFullAddress(String fullAddress){
        this.fullAddress = fullAddress;
    }

    public boolean isEnabled() {
        return enabled;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    public List<Role> getRoles() {
        return roles;
    }

    public void setRoles(List<Role> roles) {
        this.roles = roles;
    }

    @JsonIgnore
    public List<Authority> getAuths() {
        return auths;
    }

    @JsonProperty
    public void setAuths(List<Authority> auths) {
        this.auths = auths;
    }

    public List<OrgUser> getOrgUsers() {
        return orgUsers;
    }

    public void setOrgUsers(List<OrgUser> orgUsers) {
        this.orgUsers = orgUsers;
    }

    @Override
    @JsonIgnore
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    @JsonIgnore
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    @JsonIgnore
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    @JsonIgnore
    public Collection<? extends GrantedAuthority> getAuthorities() {
        if(auths!=null && auths.size()>0){
            return auths.stream().map(item ->  new SimpleGrantedAuthority(item.getCode())).collect(Collectors.toList());
        }
        return null;
    }
}